(function($){
$.weboo.view("weboo.core.list",null,null,{setup: function(view,conf){
	/* load standard setting */
	view.loadSetting(conf);

	view.values = new Array();

	/* bind cutomized event */
	if(conf.afterActive)
		view.afterActive = conf.afterActive;
	
	if(conf.beforeActive)
		view.beforeActive = conf.beforeActive;


	view.boxto = conf.boxto;

	if(conf.renderItem)
		view.renderItem = conf.renderItem;
	
	// default pattern name
	if(!view.patternName)
		view.patternName = "weboo.pattern.list.shell";

	view.itmPtn = !conf.item ? 
							"weboo.pattern.list.item"
							:conf.item;
	
	view.actPtn = conf.actived;
	
	/*  Render */
	view.beforeRender = function(){
		this.jq = this.pattern().render();
		this.drop = this.find("[plugid='drop']");
		this.box = this.find("[plugid='box']");
		this.close = this.find("[plugid='close']");
		if(this.drop.size()>0)
			this.find("[plugid='items']").hide();
		return this.jq;
	};

	view.afterRender = function(){
		if(this.drop.size()>0){
			this.drop.bind("mousedown",{view:this,fn:"dropdown"},$.weboo.fire);
			this.drop.bind("click",null,$.weboo.stopEvent);
			if(this.box.size()>0){
				this.box.bind("click",{view:this,fn:"dropdown"},$.weboo.fire);
			}
			$(document.body).bind("click",{view:this,fn:"clipup"},$.weboo.fire);
		}
		if(this.close.size()>0){
			this.close.bind("mousedown",{view:this,fn:"hide"},$.weboo.fire);
			this.close.bind("click",null,$.weboo.stopEvent);
		}
	};

	/*
	 * return jQuery object
	 */
	view.render = function(){
		// release all resources
		this.depose();
		// combine all sub views
		this.init();
		// Before render: client can override default by set this.jq.
		this.beforeRender();
		this.bindEvents();
		// AFTER RENDER: client can do anything he want.
		this.afterRender();

		this.renderItems();
		return this.jq;
	};

	/* actions */
	view.dropdown = function(){
		this.find("[plugid='items']").fadeIn(this.speed.show);
	};

	view.clipup = function(){
		this.find("[plugid='items']").fadeOut(this.speed.hide);
	};

	view.renderItems = function(){
		if(!this.values || this.values.length==0)
			return;
		/* prepare normal item pattern */
		var item = $.weboo.pattern(this.itmPtn);
		var arena = this.jq.attr("plugid")=="items" ? 
						this.jq
						:this.jq.find("[plugid='items']");
		// clear
		this.disactive();
		arena.empty();
		// render each
		var arr = this.values;
		for(var i in arr)
			arena.append(this.fn.renderByPtn(this,item,arr[i],this.renderItem));
		// draw actived
		this.active(this.aIndex);
	};

	view.fn = {
		renderByPtn: function(view,ptn,obj,func){
			var re = ptn.setObject(obj).render();
			if(func)
				func(re,obj);
			var pp = re.find(":not(:checkbox)");
			pp.bind("mousedown",{view:view,fn:"onClick"},$.weboo.fire);
			pp.bind("click",null,$.weboo.stopEvent);
			re.find(":checkbox").bind("click",null,$.weboo.stopEvent);
			return re;
		},

		replaceItem: function(org,content){
			/* IE6 special */
			if( $.browser.msie && $.browser.version=="6.0"){
				var cbs = org.find(":checkbox");
				var arr = null;
				if(cbs.size()>0){
					arr = new Array();
					cbs.each(function(){
						arr.push(this.checked);
					});
				}
				org.replaceWith(content);
				if(arr){
					cbs = content.find(":checkbox");
					for(var i in arr){
						cbs[i].checked = arr[i];
					}
				}
			}else{
				org.replaceWith(content);
			}
		}
	};

	view.depose = function(){
		if(this.jq){
			var me = this.jq.remove();
			this.jq = null;
			return me;
		}
		this.actived = null;
		this.aIndex = -1;
		return null;
	},

	view.disactive = function(){
		if(!(this.aIndex>=0) || !this.actPtn)
			return;
		if(this.actived){
			var content = this.fn.renderByPtn(this,$.weboo.pattern(this.itmPtn),this.value(),this.renderItem);
			this.fn.replaceItem(this.actived,content);
		}
		this.actived = null;
		this.aIndex = -1;
	};

	view.active = function(index){
		if(!(index>=0) || !this.actPtn)
			return;
		this.disactive();
		this.actived = $(this.find("[plugid='items']").children()[index]);
		this.aIndex = index;
		var content = this.fn.renderByPtn(this,$.weboo.pattern(this.actPtn),this.value(),this.renderItem)
		this.fn.replaceItem(this.actived,content);
		this.actived = content;
		return this.actived;
	};

	view.activeBy = function(pp, v){
		/* find index */
		var pps = this.find("[plugid='" + pp + "']");
		if(pps.size()==0)
			return;
		for(var i=0;i<pps.size();i++)
			if($.weboo.val(pps[i]) == v){
				this.active(i);
				break;
			}
	};

	view.onClick = function(event){
		var ele = $(event.target);
		var p  =  ele.parent();
		while(p.attr("plugid")!="items"){
			ele = p;
			p = ele.parent();
		}
		this.click(p.children().index(ele[0]));
	};

	view.click = function(index){
		var me = this;
		var doActive = function(){
			// change the actived item
			me.active(index);
			// fire cutomized event
			if(me.afterActive){
				me.afterActive(me.actived,me.value());
			}
			// change the actived item
			me.active(index);
			// update box value
			if(me.box.size()>0)
				me.box.text(me.value()[me.boxto ? me.boxto : "name"]);
			// if drop down list, hide shell
			if(me.drop.size()>0)
				me.clipup();
		};
		if(this.beforeActive){
			var obj = $(this.find("[plugid='items']").children()[index]);
			var v = $.weboo.valobj(obj);
			this.beforeActive(doActive,obj,v);
		}
		else
			doActive();
	};

	view.selectAll = function(){
		if(!this.jq) return;
		this.jq.find(":checkbox").find(":enabled").each(function(index,ele){
			ele.checked = true;
		});
	};

	view.cancelAll = function(){
		if(!this.jq) return;
		this.jq.find(":checkbox").find(":enabled").each(function(index,ele){
			ele.checked = false;
		});
	};

	view.reverseAll = function(){
		if(!this.jq) return;
		this.jq.find(":checkbox").find(":enabled").each(function(index,ele){
			ele.checked = !ele.checked;
		});
	};

	// REFRESH
	view.refresh = function(callback){if(!conf.dest_query) throw "Expect a url for refresh items"
		if(!this.jq)
			this.redraw();
		var me = this;
		$.weboo.ajax({
			url			: $.weboo.url(conf.dest_query)
			,data		: this.data()
			,processData: true
			,cache		: false
			,async		: !$.weboo.undefined(callback)
			,type		: "POST"
			,dataType	: "json"
			,success	: function(json){
				me.data(json);
				me.renderItems();
				if(callback)
					callback(me);
			}
		});
	};

	// DELETE
	view.delChecked = function(callback){if(!conf.dest_delete) throw "Expect a url for delete checked items"
		if(!this.jq)
			if(callback)
				callback();
		var me = this;
		var ids = this.getChecked(conf.delete_by ? conf.delete_by : "id");
		if(ids.length==0){
			if(callback)
					callback(me);
			return;
		}
		$.weboo.ajax({
			url			: $.weboo.url(conf.dest_delete)
			,data		: {ids : ids.toString()}
			,processData: true
			,cache		: false
			,async		: !$.weboo.undefined(callback)
			,type		: "POST"
			,dataType	: "json"
			,success	: function(json){
				me.refresh(function(){
					if(callback)
						callback(me);
				});
			}
		});
	};

	view.display = function(callback){
		if(!this.jq)
			this.redraw();
		if(this.cached && this.values.length > 0)
			this.renderItems();
		else
			this.refresh(callback);
		this.show();
	};

	view.value = function(){
		if(this.actived)
			return $.weboo.valobj(this.actived);
		return null;
	};

	view.getChecked = function(att){
		if(!this.jq) return null;
		var children = this.jq.find("[plugid='items']").children();	
		var re = new Array();
		if (att){
			children.each(function(index,eleDom){
				var cc = $(eleDom).find("[plugid='check']");
				if(cc.size()>0 && $.weboo.val(cc[0])==true){
					var vo = $.weboo.valobj(eleDom);
					var v = vo[att];
					if(!$.weboo.undefined(v))
						re.push(v);
				}
			});
		}
		else{
			children.each(function(index,eleDom){
				var cc = $(eleDom).find("[plugid='check']");
				if(cc.size()>0 && $.weboo.val(cc[0])==true)
					re.push($.weboo.valobj(eleDom));
			});
		}
		return re;
	};

	// load data / get query data
	view.data = function(d){
		if(!d){
			return null;
		}
		else{
			if($.weboo.isArray(d))
				this.values = d;
			else if(!$.weboo.undefined(d.list) && !$.weboo.undefined(d.pager))
				this.values = d.list;
			else
				this.values = new Array(d);
		}
	};

	view.blank = function(){
		this.children.eachValue(function(chdView){
			chdView.blank();
		});
		this.values = new Array();
	};
}});
})(jQuery);